package com.softaria.windows.logic;

import com.softaria.windows.model.PerspectiveUrl;
import com.softaria.windows.model.ViewUrl;

public class URLParser {

  /**
   * Parses URL to PerpectiveUrl or ViewUrl
   * 
   * @param historyToken
   * @return url of perspective or view
   */
  public static PerspectiveUrl parse(String historyToken) {
    if (historyToken == null)
      return null;

    historyToken = historyToken.trim();

    if (historyToken.length() == 0)
      return null;

    if (historyToken.startsWith("#")) {
      historyToken = historyToken.substring(1);
    }

    int index = historyToken.indexOf('?');

    String parameters = "";
    String beforeQuestion = "";

    if (index == -1) {
      // No parameters
      beforeQuestion = historyToken;
    } else {
      beforeQuestion = historyToken.substring(0, index);
      parameters = historyToken.substring(index + 1);
    }

    index = beforeQuestion.indexOf('/');

    if (index == -1) {
      // perspective
      return new PerspectiveUrl(beforeQuestion);
    } else {
      // view
      return new ViewUrl(beforeQuestion.substring(0, index), beforeQuestion.substring(index + 1),
          parameters);
    }

  }

}
